<%@page import="warehouse.StaffBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>职工新增或修改操作</title>
	<link rel="stylesheet" href="layui/css/layui.css">
	<script src="layui/layui.js"></script>
</head>
<body>
<%
	//获取参数
	/*
	姓名、性别、年龄、电话号码、所属仓库号
	*/
	request.setCharacterEncoding("UTF-8");
	String staffNoString = request.getParameter("staffNo");
	String name = request.getParameter("name");
	String genderString = request.getParameter("gender");
	String ageString = request.getParameter("age");
	String telenumber = request.getParameter("telenumber");
	String warehouseNoString = request.getParameter("warehouseNo");
	
	String errUrl = "staff_update_form.jsp";//错误时跳转到的url
	String successUrl = "staff_list.jsp";//成功时跳转到的url

	
	//参数类型转换
	boolean gender = "true".equals(genderString)?true:false;
	int age = 0;
	if(ageString!=null){
		age =Integer.valueOf(ageString);
	}
	int warehouseNo = 0;
	if(warehouseNoString!=null){
		warehouseNo = Integer.valueOf(warehouseNoString);
	}
	int staffNo = Integer.valueOf(staffNoString);
	
	StaffBean staff = new StaffBean(staffNo,name,gender,age,telenumber,warehouseNo);
	
	//根据staffNo的值判断要进行的操作，0为添加，负数为删除，正数为修改
	if(staffNo == 0){
		staffNo = StaffBean.addStaff(staff);
		if(staffNo == -1){
			//如果添加失败
			%>
			<script>
			layui.use('layer', function(){
				  var layer = layui.layer;
				  layer.alert('添加失败', function(index){
						window.location.href="<%=errUrl%>";
					});
				});  
			</script>
			<%
		}else{
			//如果添加成功
			%>
			<script>
			layui.use('layer', function(){
				  var layer = layui.layer;
				  layer.alert('添加成功！', function(index){
						window.location.href="<%=successUrl%>";
					});
				});  
			</script>
			<%
		}
	}else if(staffNo>0){
		boolean success = StaffBean.modStaff(staff);
		if(!success){
			//修改失败
			%>
			<script>
			layui.use('layer', function(){
				  var layer = layui.layer;
				  layer.alert('修改失败', function(index){
						window.location.href="<%=errUrl%>";
					});
				});  
			</script>
			<%
		}else{
			//修改成功
			%>
			<script>
			layui.use('layer', function(){
				  var layer = layui.layer;
				  layer.alert('修改成功！', function(index){
						window.location.href="<%=successUrl%>";
					});
				});  
			</script>
			<%
		}
	}else{
		//staffNo为负数，则删除staffNo的绝对值所对应的职工信息
		staffNo = Math.abs(staffNo);
		boolean success = StaffBean.delStaff(staffNo);
		if(!success){
			//删除失败
			%>
			<script>
			layui.use('layer', function(){
				  var layer = layui.layer;
				  layer.alert('删除失败', function(index){
						window.location.href="<%=errUrl%>";
					});
				});  
			</script>
			<%
		}else{
			//删除成功
			%>
			<script>
			layui.use('layer', function(){
				  var layer = layui.layer;
				  layer.alert('删除成功！', function(index){
						window.location.href="<%=successUrl%>";
					});
				});  
			</script>
			<%
		}
	}
	
	
	

%>
</body>
</html>